import {ADD_TODO_ITEM, CHANGE_INPUT_VALUE, DELETE_TODO_ITEM, INITIAL_ITEM_ACTION} from './actionType';
import axios from 'axios';


export const getChangeInputValueAction = (value) => ({
    type: CHANGE_INPUT_VALUE,
    value
});

export const getAddTodoItemAction = () => ({type: ADD_TODO_ITEM});

export const getDeleteTodoItemAction = (index) => ({
    type: DELETE_TODO_ITEM,
    index
});

export const initialItemAction = (value) => ({
    type: INITIAL_ITEM_ACTION,
    value
});

// once you return a function as an action,
// you have a dispatch function as a param to use
export const getTodoList = () => {
    return (dispatch) => {
        axios.get('/api/todolist')
            .then((res) => {
                const action = initialItemAction(res.data);
                dispatch (action);
            })
        // .catch(alert('error happens'))
    }
};



